package com.angel.webapp.servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.angel.architecture.exceptions.NonBusinessException;
import com.angel.common.keyValue.KeyValueResult;
import com.angel.io.descriptor.ExportedFileDescriptor;
import com.angel.webapp.file.FileInformation;
import com.angel.webapp.keyValue.SearchParametersKeyValueResult;
import com.angel.webapp.manager.ExportManager;

/**
 * Servlet to download files from exports domain objects.
 *
 * @author William
 * @since 13/August/2009
 */
public class DownloadExportFileServlet extends DownloadServlet {

    protected static final String REQ_PARAMETER_FILE_PROCESSOR_NAME = "fileProcessorName";

    protected static final String REQ_PARAMETER_SEARCH_STRATEGY_NAME = "searchStrategyName";

    protected static final String REQ_PARAMETER_ROW_PROCESSOR_NAME = "rowProcessorName";

    protected static final String REQ_PARAMETER_PARAMETERS_NAME = "searchParameters";

    protected static final long serialVersionUID = -7427467996390884097L;

    @Override
    protected FileInformation doProcess(KeyValueResult keyValueResult, final HttpServletRequest request, final HttpServletResponse response) {
        String searchStrategyName = keyValueResult.getResultFor(REQ_PARAMETER_SEARCH_STRATEGY_NAME).toString();
        String exportRowProcessorName = keyValueResult.getResultFor(REQ_PARAMETER_ROW_PROCESSOR_NAME).toString();
        String exportFileProcessorName = keyValueResult.getResultFor(REQ_PARAMETER_FILE_PROCESSOR_NAME).toString();
        String parameters = keyValueResult.getResultFor(REQ_PARAMETER_PARAMETERS_NAME).toString();
        String separatorParameters = keyValueResult.getResultFor(REQ_PARAMETER_SEPARATOR_PARAMETERS).toString();

        KeyValueResult searchParameters = new SearchParametersKeyValueResult(parameters, separatorParameters);

        ExportManager exportManager = new ExportManager();
        exportManager.setExportFileCommandName(exportFileProcessorName);
        exportManager.setExportRowCommandName(exportRowProcessorName);
        exportManager.setSearchStrategyCommandName(searchStrategyName);
        exportManager.validate();

        ExportedFileDescriptor exportedFileDescriptor = exportManager.export(searchParameters);
        exportedFileDescriptor.calculateDuration();
        //this.createOutputForResponse(exportedFileDescriptor, request, response);
        return null;
    }

    @Override
    protected void doCheckParameters(KeyValueResult keyValueResult) {
    	super.doCheckParameters(keyValueResult);
        boolean containsParameterFileProcessorName = keyValueResult.containsResultFor(REQ_PARAMETER_FILE_PROCESSOR_NAME);
        boolean containsParameterRowProcessorName = keyValueResult.containsResultFor(REQ_PARAMETER_ROW_PROCESSOR_NAME);
        boolean containsParameterSearchStrategyName = keyValueResult.containsResultFor(REQ_PARAMETER_SEARCH_STRATEGY_NAME);

        if(!containsParameterFileProcessorName || !containsParameterRowProcessorName || !containsParameterSearchStrategyName){
            String errorMessage = "Parameters necesary for export process are not present: " +
                    "Parameter Name: " + REQ_PARAMETER_FILE_PROCESSOR_NAME + " Included: " + containsParameterFileProcessorName +
                    "Parameter Name: " + REQ_PARAMETER_ROW_PROCESSOR_NAME + " Included: " + containsParameterRowProcessorName +
                    "Parameter Name: " + REQ_PARAMETER_SEARCH_STRATEGY_NAME + " Included: " + containsParameterSearchStrategyName;
            throw new NonBusinessException(errorMessage);
        }
    }
}